package me.flyray.bsin.server.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import me.flyray.bsin.domain.entity.WalletAccount;
import me.flyray.bsin.infrastructure.mapper.WalletAccountMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

/**
 * 缓存钱包服务
 */
@Slf4j
@Service
public class CachedWalletService {
    
    @Autowired
    private WalletAccountMapper walletAccountMapper;
    
    /**
     * 根据地址获取钱包账户（带缓存）
     */
    @Cacheable(value = "walletAccount", key = "#address")
    public WalletAccount getWalletAccountByAddress(String address) {
        log.debug("从数据库查询钱包账户: {}", address);
        return walletAccountMapper.selectOne(
            new LambdaQueryWrapper<WalletAccount>()
                .eq(WalletAccount::getAddress, address)
        );
    }
    
    /**
     * 更新钱包账户（清除缓存）
     */
    @CacheEvict(value = "walletAccount", key = "#address")
    public void updateWalletAccount(String address, WalletAccount account) {
        log.debug("更新钱包账户并清除缓存: {}", address);
        walletAccountMapper.updateById(account);
    }
    
    /**
     * 清除指定地址的缓存
     */
    @CacheEvict(value = "walletAccount", key = "#address")
    public void evictWalletAccountCache(String address) {
        log.debug("清除钱包账户缓存: {}", address);
    }
    
    /**
     * 清除所有钱包账户缓存
     */
    @CacheEvict(value = "walletAccount", allEntries = true)
    public void evictAllWalletAccountCache() {
        log.debug("清除所有钱包账户缓存");
    }
}
